package cn.chuan.keygen.module.verify.controller.admin.card;

import cn.chuan.keygen.module.verify.enums.ClosureEnum;
import cn.chuan.keygen.module.verify.enums.SellStatusEnum;
import cn.chuan.keygen.module.verify.enums.UseStatusEnum;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.chuan.keygen.framework.common.pojo.PageParam;
import cn.chuan.keygen.framework.common.pojo.PageResult;
import cn.chuan.keygen.framework.common.pojo.CommonResult;
import cn.chuan.keygen.framework.common.util.object.BeanUtils;
import static cn.chuan.keygen.framework.common.pojo.CommonResult.success;

import cn.chuan.keygen.framework.excel.core.util.ExcelUtils;

import cn.chuan.keygen.framework.apilog.core.annotation.ApiAccessLog;
import static cn.chuan.keygen.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.chuan.keygen.module.verify.controller.admin.card.vo.*;
import cn.chuan.keygen.module.verify.dal.dataobject.card.CardDO;
import cn.chuan.keygen.module.verify.service.card.CardService;

@Tag(name = "管理后台 - 充值卡")
@RestController
@RequestMapping("/verify/card")
@Validated
public class CardController {

    @Resource
    private CardService cardService;

    @PostMapping("/create")
    @Operation(summary = "创建充值卡")
    @PreAuthorize("@ss.hasPermission('verify:card:create')")
    public CommonResult<Long> createCard(@Valid @RequestBody CardSaveReqVO createReqVO) {
        return success(cardService.createCard(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新充值卡")
    @PreAuthorize("@ss.hasPermission('verify:card:update')")
    public CommonResult<Boolean> updateCard(@Valid @RequestBody CardSaveReqVO updateReqVO) {
        cardService.updateCard(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除充值卡")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('verify:card:delete')")
    public CommonResult<Boolean> deleteCard(@RequestParam("id") Long id) {
        cardService.deleteCard(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得充值卡")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('verify:card:query')")
    public CommonResult<CardRespVO> getCard(@RequestParam("id") Long id) {
        CardDO card = cardService.getCard(id);
        return success(BeanUtils.toBean(card, CardRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得充值卡分页")
    @PreAuthorize("@ss.hasPermission('verify:card:query')")
    public CommonResult<PageResult<CardRespVO>> getCardPage(@Valid CardPageReqVO pageReqVO) {
        PageResult<CardDO> pageResult = cardService.getCardPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CardRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出充值卡 Excel")
    @PreAuthorize("@ss.hasPermission('verify:card:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCardExcel(@Valid CardPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CardDO> list = cardService.getCardPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "充值卡.xls", "数据", CardRespVO.class,
                        BeanUtils.toBean(list, CardRespVO.class));
    }

}